"""
Case Type   : Compatibility
Case Name   : 向二级分区表中导入数据
Create At   : 2023/06/02
Owner       : n@ningyali
Description :
        1.创建二级分区表
        2.导入合规数据
        3.导入不合规数据
Expect      :
        1.成功
        2.成功
        3.合理报错
History     :
"""
import os
import unittest

from yat.test import Node

from testcase.COMPATIBILITY.GRAMMAR.LOAD. \
    Opengauss_Compatibility_Grammar_Load_Common import LoadData
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class LoadData0064(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.load_data = LoadData()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.pri_user = Node('PrimaryDbUser')
        self.case_lable = 'load_data_0064'
        self.db_name = f'db_{self.case_lable}'
        self.t_name = f't_{self.case_lable}'
        self.file_path = os.path.join('/home', self.pri_user.ssh_user)
        self.cols = 'c_01 int primary key,c_02 char(10) unique'
        self.partitions = "partition p1 values less than (1)(" \
                          "subpartition p11 values('a')," \
                          "subpartition p12 values('b')," \
                          "subpartition p13 values('c'))," \
                          "partition p2 values less than (5)" \
                          "(subpartition p21 values('a')," \
                          "subpartition p22 values('b')," \
                          "subpartition p23 values('c'))," \
                          "partition p3 values less than (10)(" \
                          "subpartition p31 values('a')," \
                          "subpartition p32 values('b')," \
                          "subpartition p33 values('c'))"

        text1 = '-----创建数据文件；expect:创建成功-----'
        self.log.info(text1)
        self.data1 = "(1,'a')(2,'b')(3,'c')"
        self.data2 = "(1,'a')(6,'b')(11,'c')"
        res = self.load_data.create_data_file(self.pri_user, self.case_lable,
                                              self.data1, self.data2)
        self.assertTrue(res, '执行失败:' + text1)

        text2 = '-----创建兼容B库；expect:创建成功-----'
        self.log.info(text2)
        res = self.load_data.create_b_db(self.pri_sh, self.db_name)
        self.assertTrue(res, '执行失败:' + text2)

    def test_load(self):
        text = '--------step1：创建表；expect：建表成功--------'
        self.log.info(text)
        res = self.load_data.create_partition_table(
            self.pri_sh, self.db_name, self.t_name, self.cols,
            partition_by='range', partition_key='c_01',
            suf_params='subpartition by list(c_02)',
            partitions=self.partitions)
        self.assertTrue(res, '执行失败:' + text)

        text = '------step2：导入合规数据；expect：成功------'
        self.log.info(text)
        f_path = os.path.join(self.file_path, f'{self.case_lable}_1.txt')
        self.load_data.load_data(self.pri_sh, self.db_name,
                                 self.t_name, f_path,
                                 line_start='(', line_end=')')
        expect = [[['1', 'a']], [['2', 'b']], [['3', 'c']]]
        partitons = ['p21', 'p22', 'p23']
        for p in list(zip(partitons, expect)):
            sql = f'select * from {self.t_name} subpartition ({p[0]});'
            res = self.pri_sh.execut_db_sql(sql, dbname=self.db_name)
            rows, _ = self.common.parse_sql_query_res(res)
            self.assertEqual(rows, p[1], '执行失败' + text)

        text = '--------step3：导入不合规数据；expect：合理报错--------'
        self.log.info(text)
        f_path = os.path.join(self.file_path, f'{self.case_lable}_2.txt')
        res2 = self.load_data.load_data(self.pri_sh, self.db_name,
                                        self.t_name, f_path,
                                        line_start='(', line_end=')')
        err_info = 'ERROR:  inserted partition key does not map to ' \
                   'any table partition'
        self.assertIn(err_info, res2, '执行失败' + text)

    def tearDown(self):
        self.log.info('-----清理环境-----')
        res = self.load_data.clear_env(self.pri_sh, self.pri_user,
                                       self.db_name,
                                       os.path.join(self.file_path,
                                                    f"{self.case_lable}*"))
        self.assertTrue(res, '执行失败:清理环境')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
